<script>
/* eslint-disable */
import Vue from 'vue';
import { configList, bindingEquipment,versionCheck } from '@/api/basic';
import { verifyAccessToken } from '@/api/login'
import { mapMutations } from 'vuex';
import {
		getChildShipByCstmId
	} from '@/api/login';
import indexConfig from '@/config/index.config';
// #ifdef APP-PLUS
import APPUpdate from "@/js_sdk/zhouWei-APPUpdate/APPUpdate";
// #endif
export default {
	async onLaunch() {
		// #ifdef APP-PLUS
			APPUpdate(true);
		// #endif
		await this.initData();			
	},
	async onShow() {
		//#ifdef APP-PLUS
		var userInfo = uni.getStorageSync('userInfo')
		var params = {
			cstm_id:userInfo.data.userId
		}
		await this.$http
			.post(`${getChildShipByCstmId}`, params)
			.then(r => {
				if(r.data.success){
					uni.setStorageSync('userInfo',r.data.obj)
				}
			})
		.catch(() => {
			this.loading = false;
		});
		// #endif
	},
	methods: {
		...mapMutations(['setCartNum', 'setNotifyNum']),
		// 数据初始化
		async initData() {
			// 获取页面设置配置
			const token = uni.getStorageSync('accessToken');
			// 获取系统title高度
			await this.initSystemInfo();
			/*if (token) {
				await this.handleVerifyAccessToken(token);
			}*/
			if (this.$mStore.getters.hasLogin) {
				// 初始化购物车数量
				this.setCartNum(uni.getStorageSync('cartNum') || 0);
				this.setNotifyNum(uni.getStorageSync('notifyNum') || 0);

			}
			// #ifdef H5
			await this.$mPayment.wxConfigH5(window.location.href);
			// #endif
		},
		//极光推送初始化
		setJpushInfo(){
			const jv = uni.requireNativePlugin('JG-JVerification');
			let that = this;	
			let userInfo = uni.getStorageInfoSync('userInfo');
			let userId = userInfo && userInfo.userId;
            if(userId == 0){
				return;
			}
			let userTag = this.$mConfig.appid+"_"+userId;				
			uni.getSystemInfo({
				success(e) {
					if (e.platform === 'android') {
						userTag = this.$mConfig.appid+"_"+userId;
					}else{
						userTag = "I"+this.$mConfig.appid+"_"+userId;
					}	
				}
			})				
			jv.init({
					timeout:7000,
					isProduction:false,
				},result=>{
					let code = result.code
					let desc = result.content
				});			
			
				
				
		 },	
		//unipush初始化
		setUniPush(){
			// #ifdef APP-PLUS
			const info = plus.push.getClientInfo();
			console.log("uni-push信息: " + JSON.stringify(info));
			// #endif
			
		},	 
		//更新用户通道
		 saveChannel(){
		  
			var BASE_URL = getBaseUrl();
			var corpId = getCorpId();
			var userId = getUserId();
			var userType = getUserType();
			var userTag = appId+"_";
			var channelId = 0;
			var systemType = api.systemType; 
			 if (userId == 0){
				 return ;
			 }
			 if (systemType == 'ios'){
					 userTag = "I"+appId+"_"+userId;
					 channelId = 1;
			 }else {
					 userTag = appId+"_"+userId;
					 channelId = 0;
			}
			api.ajax({
			url: BASE_URL+'/appController.do?saveUserChannel', 
			method: 'post',
			data: {
				values: {
					userId: userId,
					userType: userType,
					msgUid:userTag,
					channelId:channelId
				  
				  }
			  }
			}, function(ret, err) {
			  if (ret) {
					 toast('信息推送通道,更新成功！','middle');
			   } else {
			   // api.alert({ msg: JSON.stringify(err) });
				 toast('网络请求失败,请稍后再试...','middle');
			  }
			});
		 },		 
		//APP检查更新
		checkUpdate(){
			//#ifdef APP-PLUS  			 
			/*this.$http.get(`${versionCheck}`).then(r => {
			   if(plus.runtime.versionCode(r.data.version) > parseInt(plus.runtime.versionCode) ){
				   uni.showModal({ //提醒用户更新
					title: "更新提示",  
					content: r.data.note,  
					success: (res) => {  
						if (res.confirm) {  
							plus.runtime.openURL(res.data.url);  
						}  
					}  
				   }) 
			   }else{
					this.$mHelper.toast('已经是最新版本');
				}

		  });*/	
							  
				uni.request({
					url: indexConfig+'/gz/sdcc_cstm.json', //仅为示例，并非真实接口地址。
					data: {
						text: 'uni.request'
					},
					dataType:'json',
					method : 'GET',
					success: (r) => {
						//console.log(r.data.version);
					   if(r.data.version > parseInt(plus.runtime.versionCode)){
						   /*uni.showModal({ //提醒用户更新
							title: "更新提示",  
							content: r.data.note,  
							success: (res) => {  
								if (res.confirm) {  
									plus.runtime.openURL(r.data.url);  
								}  
							}  
						   })*/
                            if(plus.networkinfo.getCurrentType()!=3){  
                                uni.showToast({  
                                    title: '有新的版本发布，检测到您目前非Wifi连接，为节约您的流量，程序已停止自动更新，将在您连接WIFI之后重新检测更新。',  
                                    mask: false,  
									content: r.data.note, 
                                    duration: 5000,  
                                    icon:"none"  
                                });  
                                return;  
                            } 
                            uni.showToast({  
                                title: '有新的版本发布，检测到您目前为Wifi连接，程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',  
                                mask: false,  
                                duration: 5000,  
								content: r.data.note, 
                                icon:"none"  
                            });	
                            var dtask = plus.downloader.createDownload( this.baseUrl+r.data.url, {}, function ( d, status ) {  
                                    // 下载完成  
                                    if ( status == 200 ) {   
                                        plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
                                            uni.showToast({  
                                                title: '安装失败',  
                                                mask: false,  
                                                duration: 1500  
                                            });  
                                        })  
                                    } else {  
                                         uni.showToast({  
                                            title: '更新失败',  
                                            mask: false,  
                                            duration: 1500  
                                         });  
                                    }    
                                });  
                            dtask.start();  																				
																				
							
					   }else{
							this.$mHelper.toast('已经是最新版本');
						}						
						
						//this.text = 'request success';
					}
				});								  
			//#endif 			
		},
		// 初始化系统信息
		initSystemInfo() {
			uni.getSystemInfo({
				success(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform === 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 43;
					}
					// #endif
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					// eslint-disable-next-line
					const custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.top - e.statusBarHeight;
					// #endif
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			});
		},
		// 检验token是否有效
		async handleVerifyAccessToken (token) {
      await this.$http.post(verifyAccessToken, { token }).then(r => {
        if (!r.data.token) {
					this.$mStore.commit('logout');
        }
      });
    }
	}
};
</script>
<style lang="scss">
// 导入colorUI
@import '/static/css/colorui/main.css';
@import '/static/css/colorui/icon.css';
@import '/static/css/colorui/animation.css';
// 导入uCharts
@import './static/css/qiun.css';
// 导入阿里巴巴矢量图标库
/*#ifdef MP*/
@import './static/css/iconfont/iconfont.css';
/*#endif*/
/*#ifndef MP*/
@import url('https://at.alicdn.com/t/font_1681579_yaiyfblc2xt.css');
/*#endif*/
@import './static/css/reset.scss';
@import './static/css/uni.scss';
//导入HX-SHOP外卖图标库
//@import "./static/css/waimai/iconfont.css";
/* #ifndef APP-PLUS-NVUE */
//@import "./static/css/waimai/main.css";
/* #endif */
uni-checkbox .uni-checkbox-input {
        border-radius: 50% !important;
        color: #ffffff !important;
    }
    
    uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked {
        color: #fff;
        border-color: rgb(0, 122, 255);
        background:rgb(0, 122, 255);
    }
    uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked:after{
        font-size: 18px;
    }
</style>
